package com.bw.t1.flume_intercepter;


import com.bw.t1.utl.JSONutils;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;

public class flume_interceptor implements Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {
        byte[] body = event.getBody();
        String s = new String(body, StandardCharsets.UTF_8);
        if (JSONutils.isJson(s))
        {
            return event;
        }
        else {
            return null;
        }

    }

    @Override
    public List<Event> intercept(List<Event> list) {
        Iterator<Event> iterator = list.iterator();
        while (iterator.hasNext()){
            Event next = iterator.next();
            if (intercept(next)==null){
                iterator.remove();
            }
        }
        return list;
    }
public static class Builder implements Interceptor.Builder {
    @Override
    public Interceptor build() {
        return new flume_interceptor();
    }

    @Override
    public void configure(Context context) {

    }
}
    @Override
    public void close() {

    }
}
